package com.sanpu.kxe.business.login.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseEditActivity;
import com.sanpu.kxe.business.login.contract.ModifyAccountContract;
import com.sanpu.kxe.business.login.presenter.ModifyAccountPresenter;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.utils.CommonUtils;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.utils.SoftInputUtil;
import com.sanpu.kxe.utils.SystemBarHelper;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Description：修改登录手机号
 * Created by 薛勇军 on 2019/3/22.
 */

public class ModifyAccountActivity extends BaseEditActivity implements ModifyAccountContract.View {

    @Bind(R.id.reAccountTv)
    TextView reAccountTv;
    @Bind(R.id.newAccountEt)
    EditTextWithDel newAccountEt;
    @Bind(R.id.checkCodeEt)
    EditTextWithDel checkCodeEt;
    @Bind(R.id.nameEt)
    EditTextWithDel nameEt;
    @Bind(R.id.identityNumberEt)
    EditTextWithDel identityNumberEt;
    @Bind(R.id.passwordEt)
    EditTextWithDel passwordEt;
    @Bind(R.id.sendCodeRtv)
    TextView sendCodeRtv;
    @Bind(R.id.commit)
    RoundTextView commit;
    @Bind(R.id.headerView)
    LinearLayout headerView;

    private ModifyAccountContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_modify_account;
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) headerView.getLayoutParams();
        params.setMargins(0, barHeight, 0, 0);
        headerView.setLayoutParams(params);

        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            reAccountTv.setText(CommonUtils.secretPhoneNumber(centerOutput.getUserName()));
        }

        mPresenter = new ModifyAccountPresenter(this);
    }

    @Override
    protected void setListener() {
        super.setListener();
        ButterKnife.findById(this, R.id.leftLl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        RxView.clicks(sendCodeRtv).throttleFirst(3, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                String phoneNumber = newAccountEt.getText().toString();
                if (TextUtils.isEmpty(phoneNumber)) {
                    ToastUtil.showTextViewPrompt("新手机号码不能为空");
                    return;
                }
                if (VerifyUtil.checkPhoneNumber(phoneNumber)) {
                    ToastUtil.showTextViewPrompt("请输入正确的手机号");
                    return;
                }
                mPresenter.onSendCode(phoneNumber);
            }
        });
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newAccount = newAccountEt.getText().toString();
                String checkCode = checkCodeEt.getText().toString();
                String name = nameEt.getText().toString();
                String identityNumber = identityNumberEt.getText().toString();
                String password = passwordEt.getText().toString();
                mPresenter.updateAccount(newAccount, checkCode, name, identityNumber, password);
            }
        });
    }

    @Override
    public void changeByStatus(boolean status) {
        if (status) {
            sendCodeRtv.setEnabled(false);
            sendCodeRtv.setTextColor(ContextCompat.getColor(mContext, R.color.c_cccccc));
        } else {
            sendCodeRtv.setEnabled(true);
            sendCodeRtv.setText("获取验证码");//数据发送完后设置为原来的文字
            sendCodeRtv.setTextColor(ContextCompat.getColor(mContext, R.color.color_base));
        }
    }

    @Override
    public void byoyomi(Long aLong) {
        sendCodeRtv.setText(aLong + "秒");
    }

    @Override
    protected void onPause() {
        super.onPause();
        SoftInputUtil.hideSoftInput(this);
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(ModifyAccountContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
